// 自定义底部导航栏逻辑
Component({
  data: {
    selected: 0,
    color: "#8a8a8a",
    selectedColor: "#4CAF50",
    list: [
      {
        pagePath: "/pages/list-user/list-user",
        icon: "📋",
        text: "任务列表"
      },
      {
        pagePath: "/pages/profile/profile",
        icon: "👤",
        text: "个人中心"
      }
    ]
  },

  /**
   * 组件准备完毕
   */
  ready() {
    // 根据用户角色设置正确的list页面
    this.updateListByRole()
    // 获取当前页面路径，设置对应的选中状态
    this.setSelected()
  },

  methods: {
    /**
     * 根据用户角色更新列表页面路径
     */
    updateListByRole() {
      try {
        const userRole = wx.getStorageSync('userRole')
        let newList = []
        
        if (userRole === 'user') {
          // 用户端：任务列表、呼叫救援、个人中心
          newList = [
            {
              pagePath: "/pages/list-user/list-user",
              icon: "📋",
              text: "任务列表"
            },
            {
              pagePath: "/pages/order/order",
              icon: "🚑",
              text: "呼叫救援"
            },
            {
              pagePath: "/pages/profile/profile",
              icon: "👤",
              text: "个人中心"
            }
          ]
        } else if (userRole === 'medical') {
          // 医护端：任务列表、个人中心
          newList = [
            {
              pagePath: "/pages/list-medical/list-medical",
              icon: "📋",
              text: "任务列表"
            },
            {
              pagePath: "/pages/profile/profile",
              icon: "👤",
              text: "个人中心"
            }
          ]
        } else if (userRole === 'driver') {
          // 司机端：任务列表、个人中心
          newList = [
            {
              pagePath: "/pages/list-driver/list-driver",
              icon: "📋",
              text: "任务列表"
            },
            {
              pagePath: "/pages/profile/profile",
              icon: "👤",
              text: "个人中心"
            }
          ]
        } else {
          // 默认
          newList = [
            {
              pagePath: "/pages/list-user/list-user",
              icon: "📋",
              text: "任务列表"
            },
            {
              pagePath: "/pages/profile/profile",
              icon: "👤",
              text: "个人中心"
            }
          ]
        }
        
        this.setData({ list: newList })
      } catch (e) {
        console.log('获取用户角色失败:', e)
      }
    },

    /**
     * 切换标签页
     */
    switchTab(e: any) {
      const data = e.currentTarget.dataset
      const url = data.path
      const index = data.index
      
      // 更新选中状态
      this.setData({
        selected: index
      })

      // 切换页面
      wx.switchTab({ url })
    },

    /**
     * 设置当前选中的标签
     */
    setSelected() {
      const pages = getCurrentPages()
      const current = pages[pages.length - 1]
      const url = current.route
      
      const selected = this.data.list.findIndex(item => 
        item.pagePath === `/${url}`
      )
      
      if (selected !== -1) {
        this.setData({
          selected
        })
      }
    },

    /**
     * 更新选中状态（供外部调用）
     */
    updateSelected(selected: number) {
      this.setData({
        selected
      })
    }
  }
})
